home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / Alpha ƒ / Help / Regular Expressions < prev    next >
Text File  |  1996-01-05  |  8KB  |  167 lines

  1. NOTE:     The following is the manual page supplied with the regular expression
  2.         routines used both in Alpha and Tcl. - pete.
  3.  
  4.  
  5.  
  6.      NAME
  7.           regcomp, regexec, regsub, regerror - regular expression
  8.           handler
  9.  
  10.      SYNOPSIS
  11.           #include <regexp.h>
  12.  
  13.           regexp *regcomp(exp)
  14.           char *exp;
  15.  
  16.           int regexec(prog, string)
  17.           regexp *prog;
  18.           char *string;
  19.  
  20.           regsub(prog, source, dest)
  21.           regexp *prog;
  22.           char *source;
  23.           char *dest;
  24.  
  25.           regerror(msg)
  26.           char *msg;
  27.  
  28.      DESCRIPTION
  29.           These functions implement egrep(1)-style regular expressions
  30.           and supporting facilities.
  31.  
  32.           Regcomp compiles a regular expression into a structure of
  33.           type regexp, and returns a pointer to it.  The space has
  34.           been allocated using malloc(3) and may be released by free.
  35.  
  36.           Regexec matches a NUL-terminated string against the compiled
  37.           regular expression in prog.  It returns 1 for success and 0
  38.           for failure, and adjusts the contents of prog's startp and
  39.           endp (see below) accordingly.
  40.  
  41.           The members of a regexp structure include at least the
  42.           following (not necessarily in order):
  43.  
  44.                char *startp[NSUBEXP];
  45.                char *endp[NSUBEXP];
  46.  
  47.           where NSUBEXP is defined (as 10) in the header file.  Once a
  48.           successful regexec has been done using the regexp, each
  49.           startp-endp pair describes one substring within the string,
  50.           with the startp pointing to the first character of the
  51.           substring and the endp pointing to the first character
  52.           following the substring.  The 0th substring is the substring
  53.           of string that matched the whole regular expression.  The
  54.           others are those substrings that matched parenthesized
  55.           expressions within the regular expression, with
  56.           parenthesized expressions numbered in left-to-right order of
  57.           their opening parentheses.
  58.  
  59.           Regsub copies source to dest, making substitutions according
  60.           to the most recent regexec performed using prog.  Each
  61.           instance of `&' in source is replaced by the substring
  62.           indicated by startp[0] and endp[0].  Each instance of `\n',
  63.           where n is a digit, is replaced by the substring indicated
  64.           by startp[n] and endp[n].  To get a literal `&' or `\n' into
  65.           dest, prefix it with `\'; to get a literal `\' preceding `&'
  66.           or `\n', prefix it with another `\'.
  67.  
  68.           Regerror is called whenever an error is detected in regcomp,
  69.           regexec, or regsub.  The default regerror writes the string
  70.           msg, with a suitable indicator of origin, on the standard
  71.           error output and invokes exit(2).  Regerror can be replaced
  72.           by the user if other actions are desirable.
  73.  
  74.      REGULAR EXPRESSION SYNTAX
  75.           A regular expression is zero or more branches, separated by
  76.           `|'.  It matches anything that matches one of the branches.
  77.  
  78.           A branch is zero or more pieces, concatenated.  It matches a
  79.           match for the first, followed by a match for the second,
  80.           etc.
  81.  
  82.           A piece is an atom possibly followed by `*', `+', or `?'.
  83.           An atom followed by `*' matches a sequence of 0 or more
  84.           matches of the atom.  An atom followed by `+' matches a
  85.           sequence of 1 or more matches of the atom.  An atom followed
  86.           by `?' matches a match of the atom, or the null string.
  87.  
  88.           An atom is a regular expression in parentheses (matching a
  89.           match for the regular expression), a range (see below), `.'
  90.           (matching any single character), `^' (matching the null
  91.           string at the beginning of the input string), `$' (matching
  92.           the null string at the end of the input string), a `\'
  93.           followed by a single character (matching that character), or
  94.           a single character with no other significance (matching that
  95.           character).
  96.  
  97.           A range is a sequence of characters enclosed in `[]'.  It
  98.           normally matches any single character from the sequence.  If
  99.           the sequence begins with `^', it matches any single
  100.           character not from the rest of the sequence.  If two
  101.           characters in the sequence are separated by `-', this is
  102.           shorthand for the full list of ASCII characters between them
  103.           (e.g. `[0-9]' matches any decimal digit).  To include a
  104.           literal `]' in the sequence, make it the first character
  105.           (following a possible `^').  To include a literal `-', make
  106.           it the first or last character.
  107.  
  108.      AMBIGUITY
  109.           If a regular expression could match two different parts of
  110.           the input string, it will match the one which begins
  111.           earliest.  If both begin in the same place    but match
  112.           different lengths, or match the same length in different
  113.           ways, life gets messier, as follows.
  114.  
  115.           In general, the possibilities in a list of branches are
  116.           considered in left-to-right order, the possibilities for
  117.           `*', `+', and `?' are considered longest-first, nested
  118.           constructs are considered from the outermost in, and
  119.           concatenated constructs are considered leftmost-first.  The
  120.           match that will be chosen is the one that uses the earliest
  121.           possibility in the first choice that has to be made.  If
  122.           there is more than one choice, the next will be made in the
  123.           same manner (earliest possibility) subject to the decision
  124.           on the first choice.  And so forth.
  125.  
  126.           For example, `(ab|a)b*c' could match `abc' in one of two
  127.           ways.  The first choice is between `ab' and `a'; since `ab'
  128.           is earlier, and does lead to a successful overall match, it
  129.           is chosen.  Since the `b' is already spoken for, the `b*'
  130.           must match its last possibility-the empty string-since it
  131.           must respect the earlier choice.
  132.  
  133.           In the particular case where no `|'s are present and there
  134.           is only one `*', `+', or `?', the net effect is that the
  135.           longest possible match will be chosen.  So `ab*', presented
  136.           with `xabbbby', will match `abbbb'.  Note that if `ab*' is
  137.           tried against `xabyabbbz', it will match `ab' just after
  138.           `x', due to the begins-earliest rule.  (In effect, the
  139.           decision on where to start the match is the first choice to
  140.           be made, hence subsequent choices must respect it even if
  141.           this leads them to less-preferred alternatives.)
  142.  
  143.      DIAGNOSTICS
  144.           Regcomp returns NULL for a failure (regerror permitting),
  145.           where failures are syntax errors, exceeding implementation
  146.           limits, or applying `+' or `*' to a possibly-null operand.
  147.  
  148.      HISTORY
  149.           Both code and manual page were written at U of T.  They are
  150.           intended to be compatible with the Bell V8 regexp(3), but
  151.           are not derived from Bell code.
  152.  
  153.      BUGS
  154.           Empty branches and empty regular expressions are not
  155.           portable to V8.
  156.  
  157.           The restriction against applying `*' or `+' to a possibly-
  158.           null operand is an artifact of the simplistic
  159.           implementation.
  160.  
  161.           Does not support egrep's newline-separated branches; neither
  162.           does the V8 regexp(3), though.
  163.  
  164.           Due to emphasis on compactness and simplicity, it's not
  165.           strikingly fast.  It does give special attention to handling
  166.           simple cases quickly.
  167.